package ray.mgocc.swing;

import ray.mgocc.net.GameAccount;

import javax.swing.JPasswordField;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.Action;
import java.awt.Panel;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;

import java.awt.event.ActionEvent;

public class LoginPanel extends JPanel{
    private Action action;
    private JTextField id = new JTextField(12);
    public LoginPanel(){
        this.action = new LoginAction();
        init();
    }
    public void init(){
        BoxLayout boxlayout = new BoxLayout(this,BoxLayout.Y_AXIS);
        setLayout(boxlayout);

        JPanel id = new JPanel();
        id.add(new JLabel("Game ID"));
        id.add(this.id);
        add(id);

        JPanel pass = new JPanel();
        pass.add(new JLabel("Password"));
        pass.add(new JPasswordField(12));
        add(pass);

        JPanel control = new JPanel();
        control.add(new JCheckBox("save"));
        control.add(new JButton(action));
        control.add(new JButton("cancel"));
        add(control);
    }
    public Action getAction(){
        return this.action;
    }
    public String getId(){
        return id.getText();
    }
    public String getPassword(){
        return "";
    }

    class LoginAction extends AbstractAction{
        private GameAccount account = new GameAccount();
        public LoginAction(){
            setup();
        }
        public String getName(){
            String name = getClass().getSimpleName();
//             int start = 0;
//             int end = name.length()-"Action".length();
//             name = name.substring(start,end);
            name = name.replaceAll("Action$","");
            return name;
        }
        public String getDescription(){
            return getName();
        }
        public int getKey(){return 0;}
        public void setup(){
            String name = getName();
            int key = getKey();
            String desc = getDescription();
            putValue(NAME,name);
            putValue(ACCELERATOR_KEY,KeyStroke.getKeyStroke
                     (key, ActionEvent.ALT_MASK));
            putValue(MNEMONIC_KEY,new Integer(key));
            putValue(ACTION_COMMAND_KEY,name);
            putValue(LONG_DESCRIPTION,desc);
            putValue(SHORT_DESCRIPTION,desc);
            //putValue(SMALL_ICON,null);
            //putValue(JComponent.TOOL_TIP_TEXT_KEY,desc);
        }
        public void actionPerformed(ActionEvent e){
            doit();
        }
        public GameAccount getAccount(){
            return this.account;
        }
        public void doit(){
            //login();
        }
    }
}

